﻿_spBodyOnLoadFunctionNames.push('MyLinksMenu.init');

var MyLinksMenu = MyLinksMenu || {};

MyLinksMenu.init = function ()
{
	MyLinksMenu.overrideMenuHtcInternal();
	MyLinksMenu.overrideCreateMenuItem();
};

MyLinksMenu.addMylinks_Dialog = function (webURL)
{
	var options =
	{
		title: MyLinksMenu.addToMyLinksText,
		showClose: true,
		allowMaximize: false,
		autoSize: true,
		url: webURL + '/_layouts/MyLinksMenu/MyLinksDialogRedirect.aspx?Title=' + document.title + '&URL=' + window.location.href,
		dialogReturnValueCallback: function (dialogResult)
		{
			SP.UI.ModalDialog.RefreshPage(dialogResult);
		}
	};
	SP.UI.ModalDialog.showModalDialog(options);
};

MyLinksMenu.manageMylinks_Dialog = function (webURL)
{
	var options =
	{
		title: MyLinksMenu.manageMyLinksText,
		showClose: true,
		allowMaximize: false,
		autoSize: true,
		url: webURL + '/_layouts/MyLinksMenu/ManageMyLinks.aspx',
		dialogReturnValueCallback: function (dialogResult)
		{
			SP.UI.ModalDialog.RefreshPage(dialogResult);
		}
	};
	SP.UI.ModalDialog.showModalDialog(options);
};

//Overriding MenuHtcInternal_Show will allow us to change the menu immediately after it's created.
MyLinksMenu.overrideMenuHtcInternal = function ()
{
	MyLinksMenu.baseMenuHtcInternal_Show = MenuHtcInternal_Show;
	MenuHtcInternal_Show = function (oMaster, oParent, y, fFlipTop)
	{
		MyLinksMenu.baseMenuHtcInternal_Show(oMaster, oParent, y, fFlipTop);
		var parentType = oParent.getAttribute('type');

		//Only proceed if this is a "MyLinksMenu"
		if ((parentType != 'submenu' && MyLinksMenu.showQuickLinks) || !MyLinksMenu.isMyLinksMenu(oMaster))
			return;

		var oPopup = oMaster._arrPopup[oMaster._nLevel];
		MyLinksMenu.fixSubMenuWidth(oPopup);
		MyLinksMenu.hideSubMenuSeparators(oPopup, oParent);
		MyLinksMenu.createRootSiteLinks(oMaster);
	};

	//Uncomment the line below to stop the menu from disappearing after it looses focus. This is very helpful for debugging & styling.
	//SetTimeOutToHideMenu = function () { }
};

//Override the CreateMenuItem function so we can save a reference to the html node that was created for the menu item.
//This reference will be used later to create an onclick event.
MyLinksMenu.overrideCreateMenuItem = function ()
{
	MyLinksMenu.baseCreateMenuItem = CreateMenuItem;
	CreateMenuItem = function (oMaster, oNode, wzID, wzHtml)
	{
		var menuTag = MyLinksMenu.baseCreateMenuItem(oMaster, oNode, wzID, wzHtml);
		oNode.menuTag = menuTag;
		return menuTag;
	};
};

MyLinksMenu.fixSubMenuWidth = function (menu)
{
	var divs = menu.getElementsByTagName('div');

	for (var i = 0; i < divs.length; i++)
	{
		if (divs[i].style.overflowX == 'auto' && !divs[i].scrollBarWidthAdded)
		{
			var scrollbarWidth = divs[i].offsetWidth - divs[i].clientWidth;
			var listTag = (!_fV4UI ? 'table' : 'ul');
			var listWidth = divs[i].getElementsByTagName(listTag)[0].offsetWidth; ;

			divs[i].style.width = (parseInt(listWidth) + scrollbarWidth) + 'px';
			divs[i].scrollBarWidthAdded = true;

			return;
		}
	}
};

//This will hide unwanted separators that appear to be automatcally inserted after submenus.
//It will also hide the "Root Site" link in a submenu if the link has already been associated with the parent element (this should happen in createRootSiteLinks).
MyLinksMenu.hideSubMenuSeparators = function (menu, oParent)
{
	var itemTag = (!_fV4UI ? 'tr' : 'li');
	var menuItems = menu.getElementsByTagName(itemTag);

	for (var i = 0; i < menuItems.length; i++)
	{
		var tempType = menuItems[i].getAttribute('type');

		if (tempType == 'separator' || tempType == null)
		{
			//Hide separators
			menuItems[i].style.display = 'none';
		}
		else if (i == 0 && tempType == 'option' && menuItems[i].getAttribute('text') == MyLinksMenu.rootSiteText)
		{
			//Hide "Root Site" link.
			if (menuItems[i].getAttribute('onmenuclick') == oParent.getAttribute('onmenuclick'))
				menuItems[i].style.display = 'none';
		}
	}
};

MyLinksMenu.createRootSiteLinks = function (oMaster)
{
	for (var i = 0; i < oMaster._oRoot.childNodes.length; i++)
	{
		var menuItem = oMaster._oRoot.childNodes[i];

		if (menuItem.menuTag)
		{
			if (menuItem.childNodes && menuItem.childNodes.length > 1)
			{
				var tempItem = menuItem.childNodes[1];

				if (tempItem.getAttribute && tempItem.getAttribute('text') == MyLinksMenu.rootSiteText)
				{
					var tempOnMenuClick = tempItem.getAttribute('onmenuclick');
					var labelTag = (!_fV4UI ? 'label' : 'a');

					var tempLabel = menuItem.menuTag.getElementsByTagName(labelTag)[0];
					tempLabel.setAttribute('onmenuclick', tempOnMenuClick);
					tempLabel.onclick = MyLinksMenu.navigateToRootSite;

					menuItem.menuTag.setAttribute('onmenuclick', tempOnMenuClick);
					menuItem.menuTag.onclick = MyLinksMenu.navigateToRootSite;
				}
			}
		}
	}
}

MyLinksMenu.navigateToRootSite = function ()
{
	var oMaster = g_menuHtc_lastMenu;
	HideMenu(oMaster, 1);
	ExecuteOnClick(this.getAttribute('onmenuclick'), oMaster);
	HideMenu(oMaster, 2);
};

MyLinksMenu.isMyLinksMenu = function (el, level)
{
	if (el.parentElement)
	{
		if (el.parentElement.id.indexOf('MyLinksMenu') > -1)
			return true;
		else
			return MyLinksMenu.isMyLinksMenu(el.parentElement, level + 1);
	}
	else
		return false;
};
